package com.wteam.dragon.model.service.impl;

import cn.hutool.core.lang.Dict;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wteam.dragon.model.mapper.ArticleMapper;
import com.wteam.dragon.model.pojo.bean.Article;
import com.wteam.dragon.model.service.ArticleService;
import com.wteam.dragon.system.basebean.bean.ResultMessage;
import com.wteam.dragon.system.exception.BadRequestException;
import com.wteam.dragon.system.util.ValidationUtil;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @Author 陈晓辉
 * @Date 2020/5/1 22:43
 */
@Service
@RequiredArgsConstructor
public class ArticleServiceImpl implements ArticleService {
    private final ArticleMapper articleMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ResultMessage addArticle(Article article) {
        articleMapper.insert(article);
        ResultMessage resultMessage = new ResultMessage();
        resultMessage.setStatus(HttpStatus.OK.value());
        resultMessage.setMessage("添加成功");
        return resultMessage;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ResultMessage delArticle(List<Long> ids) {
        if (ids == null || ids.size() == 0) {
            throw new BadRequestException("ids不能为空");
        }
        articleMapper.deleteBatchIds(ids);
        ResultMessage resultMessage = new ResultMessage();
        resultMessage.setStatus(HttpStatus.OK.value());
        resultMessage.setMessage("删除成功");
        return resultMessage;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ResultMessage editArticle(Article article) {
        articleMapper.updateById(article);
        ResultMessage resultMessage = new ResultMessage();
        resultMessage.setStatus(HttpStatus.OK.value());
        resultMessage.setMessage("更改成功");
        return resultMessage;
    }

    @Override
    public ResultMessage pageArticle(Page<Article> page,
                                     String title, Long typeId, Boolean isShow) {
        ValidationUtil.isNull("typeId", typeId);
        ValidationUtil.isNull("isShow", isShow);
        QueryWrapper<Article> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda()
                .like(title != null, Article::getTitle, title)
                .eq(Article::getTypeId, typeId)
                .eq(Article::getIsShow, isShow)
                .orderByAsc(Article::getSort);
        Page<Article> articlePage = articleMapper.selectPage(page, queryWrapper);
        Dict resultMap = Dict.create()
                .set("message", "请求成功")
                .set("articlePage", articlePage);
        ResultMessage resultMessage = new ResultMessage();
        resultMessage.setStatus(HttpStatus.OK.value());
        resultMessage.setResultParam(resultMap);
        return resultMessage;
    }

    @Override
    public ResultMessage getArticle(Long id) {
        Article article = articleMapper.selectById(id);
        Dict resultMap = Dict.create()
                .set("message", "请求成功")
                .set("article", article);
        ResultMessage resultMessage = new ResultMessage();
        resultMessage.setStatus(HttpStatus.OK.value());
        resultMessage.setResultParam(resultMap);
        return resultMessage;
    }
}
